//$CC OP_qn.c -o ectest_OP -L /home/lee/test -lethercat
//scp ectest_OP root@192.168.58.1:/home/mc
#include "config_qn.h"
/*****************************************************************************/

void check_master_state(void)
{
    ec_master_state_t ms;

    ecrt_master_state(master, &ms);

    if (ms.slaves_responding != master_state.slaves_responding) {
        printf("%u slave(s).\n", ms.slaves_responding);
    }
    if (ms.al_states != master_state.al_states) {
        printf("AL states: 0x%02X.\n", ms.al_states);
    }
    if (ms.link_up != master_state.link_up) {
        printf("Link is %s.\n", ms.link_up ? "up" : "down");
    }

    master_state = ms;
}  

/*****************************************************************************
 * Domain_reg
 ****************************************************************************/
int DomainReg(void)
{
	/********************/
    printf("xenomai Configuring PDOs...\n");    
    domainServoOutput = ecrt_master_create_domain(master);
    if (!domainServoOutput) {
        return -1;
    }
    domainServoInput = ecrt_master_create_domain(master);
    if (!domainServoInput) {
        return -1;
    }
    printf("nei cun is ok\n");
    /********************/
   

   /********************/
    if (ecrt_domain_reg_pdo_entry_list(domainServoOutput, domainServoOutput_regs)) {
        fprintf(stderr, "PDO entry registration failed!\n");
        return -1;
    }
    if (ecrt_domain_reg_pdo_entry_list(domainServoInput, domainServoInput_regs)) {
        fprintf(stderr, "PDO entry registration failed!\n");
        return -1;
    }

    fprintf(stderr, "Creating SDO requests...\n");

    // ecrt_slave_config_sdo8(sc_estun, 0x6060, 0, 8);
    // ecrt_slave_config_sdo8(sc_estun, 0x60C2, 1, 1);

    return 0;
}
/*****************************************************************************/
int main(void)
{
    
    printf("hello world!\n");
    //sleep(1);
    master = ecrt_request_master(0);
    if (!master) {
        return -1;
    }
    printf("request_master sucess\n");
    check_master_state();
    //check_slave_config_states();
    
    DomainReg();
    
    printf("release_master\n");
    ecrt_release_master(master);
    //pause();
    return 0;
}


















